package ysq.xyg.shop.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.shop.dao.ShopOrderDao;
import ysq.xyg.shop.model.po.ShopOrder;
import ysq.xyg.shop.model.vo.ShopOrderVo;
import ysq.xyg.shop.service.ShopOrderService;
import ysq.xyg.sys.model.po.SysUser;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


/**
 * 商城模块-订单生成表service实现
 *
 * @author congcong tao
 * @date 2019-5-29 16:41:51
 */
@Service("shopOrderService")
public class ShopOrderServiceImpl extends BaseServiceImpl<ShopOrder> implements ShopOrderService {

    @Resource
    private ShopOrderDao shopOrderDao;

    private ShopOrder shopOrder;

    @Autowired
    public void setBaseDao(ShopOrderDao shopOrderDao) {
        this.baseDao = shopOrderDao;
    }


    /**
     * 分页查询
     *
     * @param shopOrderVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-5-29 16:41:51
     */
    public ReturnEntity page(ShopOrderVo shopOrderVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" update_time desc");
        List<ShopOrder> list = acceptList(shopOrderVo);
        PageInfo<ShopOrder> page=new PageInfo<ShopOrder>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }

    @Override
    public ReturnEntity pageLeft(ShopOrderVo shopOrderVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        List<ShopOrder> list = shopOrderDao.selectEntity(shopOrderVo);
        PageInfo<ShopOrder> page=new PageInfo<ShopOrder>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param shopOrderVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-5-29 16:41:51
     */
    public ReturnEntity addOrUpdate(ShopOrderVo shopOrderVo, SysUser currentUser) {
        boolean isUpate=true;
        if(StrUtil.isEmpty(shopOrderVo.getId())){
            shopOrder=new ShopOrder(currentUser);
            isUpate=false;
        }else{
            shopOrder=selectByPrimaryKey(shopOrderVo.getId());
            if(ObjectUtil.isNull(shopOrder)){
                return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
            }
            shopOrder.setUpdateTime(new Date());
            shopOrder.setUpdateUser(currentUser.getId());
        }
        shopOrder.setMoney(shopOrderVo.getMoney());
        shopOrder.setNum(shopOrderVo.getNum());
        shopOrder.setDescription(shopOrderVo.getDescription());
        shopOrder.setManageTime(shopOrderVo.getManageTime());
        int num=0;
        if(isUpate){
            num=updateByPrimaryKey(shopOrder);
        }else{
            num=insert(shopOrder);
        }
        if(num<=0){
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }
    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-5-29 16:41:51
     */
    public ReturnEntity delete(String id, SysUser currentUser) {
        ShopOrder shopOrder=selectByPrimaryKey(id);
        if(shopOrder!=null){
            shopOrder.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            shopOrder.setUpdateTime(new Date());
            shopOrder.setUpdateUser(currentUser.getId());
            int num=updateByPrimaryKey(shopOrder);
            if(num>0){
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
    }

    /**
     * 获取集合
     *
     * @param shopOrderVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author congcong tao
     * @date 2019-5-29 16:41:51
     */
    public List<ShopOrder> acceptList(ShopOrderVo shopOrderVo){
        Example example = new Example(ShopOrder.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(shopOrderVo.getRemoveTag())){
            criteria.andEqualTo("removeTag",shopOrderVo.getRemoveTag());
        }
        if(StringUtil.isNotEmpty(shopOrderVo.getDescription())){
            criteria.andLike("description","%" + shopOrderVo.getDescription() + "%");
        }
        if(StringUtil.isNotEmpty(shopOrderVo.getNowDay())){
            // 时间的比较
            criteria.andBetween("createTime",shopOrderVo.getNowDay() + " 00:00:00", shopOrderVo.getNowDay() + " 23:59:59");
        }
        example.setOrderByClause(" update_time desc");
        return shopOrderDao.selectByExample(example);
    }

    @Override
    public Double selectAllCount(ShopOrderVo shopOrderVo) {
        return shopOrderDao.selectAllCount(shopOrderVo);
    }

    @Override
    public Double selectAllCountByMemberId(ShopOrderVo shopOrderVo) {
        return shopOrderDao.selectAllCountByMemberId(shopOrderVo);
    }
}
